<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Businesses;

class BusinessesController extends Controller
{
    //
    public function getBusinesses(Request $request){
        $businesses = Businesses::where('type',$request->get('type')) ->orderBy('id')-> get();
        return response([
            'data' => $businesses,
            'code' => 200
        ]);
    }

    public function addBusinesses(Request $request){
//        $new = News::where('type',$request->get('type'));
        $data = $request->data;
//        dd($data['add']);
        foreach($data['add']??[] as $key => $item){
            Businesses::create([
                'port' => $item['port'],
                'throughput' => $item['throughput'],
                'author' => $item['author'],
                'type' => $request->get('type'),
            ]);
        }
        Businesses::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            Businesses::find($item['id'])->update([
                'port' => $item['port'],
                'throughput' => $item['throughput'],
                'author' => $item['author'],
                'type' => $request->get('type'),
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
